package cn.com.code.design.agent;

import cn.com.code.design.IUserDao;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * @author zhengpei
 * @date 2021/10/23
 */
public class RegisterBeanFactory implements BeanDefinitionRegistryPostProcessor {

  @Override
  public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
    beanDefinition.setBeanClass(MapperFactoryBean.class);
    beanDefinition.setScope("singleton");
    beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(IUserDao.class);

    BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(beanDefinition, "userDao");
    BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);
  }

  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

  }
}
